import { GameLayer } from '../Libs/Framework/Enums/GameLayer';
import { ModuleNames } from '../Libs/Framework/Enums/ModuleNames';
import { GameEvent, SceneName } from '../Libs/Framework/Event/GameEvent';
import { ModuleEvent } from '../Libs/Framework/Event/ModuleEvent';
import { EventManager } from '../Libs/Framework/Managers/EventManager';
import FScene from "../Libs/Framework/Support/FScene";
import LobbyMeditor from '../Mediator/LobbyMediator';
import { GameDataManager } from '../Libs/Framework/Managers/GameDataManager';
import { SceneEvent } from '../Libs/Framework/Event/SceneEvent';
import { HelpTools } from '../Libs/Utils/HelpTools';


const { ccclass, property } = cc._decorator;
@ccclass
export default class LobbyScene extends FScene {
    public start(): void {
        this.mediator = new LobbyMeditor
        this.mediator.initialize()
        this.mediator.startMediator()
    }

    protected addEvents(): void {
        EventManager.addEvent(this, GameEvent.ON_ERROR_MSG, this.error);
        EventManager.addEvent(this,GameEvent.ON_GAME_QPMJ_CREATERSUCCESS,this.CreaterDeskSuccess)
    }

    //房间创建成功
    private CreaterDeskSuccess(data:any) : void {
        //初始化数据.
        //TODO  后期需要稍微修改一下
        let d : any = data.data.data.deskInfo
        GameDataManager.game.deskno = d.deskId
        GameDataManager.room.CreatedAt = d.createdAt
        GameDataManager.room.Creator = d.creator
        GameDataManager.room.Title = d.title
        GameDataManager.room.Desc = d.desc
        GameDataManager.room.Mode = d.mode
        GameDataManager.room.Round = d.round
        GameDataManager.room.Status = d.status

        //载入游戏场景
        EventManager.dispatchEvent(new SceneEvent(SceneEvent.CHANGE_SCENE, HelpTools.titleCase(data.data.id)  + "Scene",null));
        
    }

    //加入房间
    private onJoinDeskClick(): void {
        let data: any = { rejectOther: true, rejectCallback: ()=>{
            EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.Joindesk);
            EventManager.dispatchModuleEventWith(ModuleEvent.HIDE_MODULE, ModuleNames.Mask)
        } };
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Mask, GameLayer.WindowMask)
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Joindesk, GameLayer.Window, data);
    }

    private onCreateDeskClick(): void {
        let data: any = { rejectOther: true, rejectCallback: ()=>{
            EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.CreateDesk);
            EventManager.dispatchModuleEventWith(ModuleEvent.HIDE_MODULE, ModuleNames.Mask)
        } };
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Mask, GameLayer.WindowMask)
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.CreateDesk, GameLayer.Window, data);
    }

    private error(errInfo: any): void {
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Mask, GameLayer.WindowMask)
        EventManager.dispatchModuleEventWith(ModuleEvent.SHOW_MODULE, ModuleNames.Confirm, GameLayer.Window, {
            content: errInfo.data.data.str,
            Confirm: () => {
                EventManager.dispatchModuleEventWith(ModuleEvent.HIDE_MODULE, ModuleNames.Mask)
                EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.Confirm);
            }
        });
    }


}